<template>
  <Teleport to="body">
    <transition name="fullplay">
      <!-- <div class="fullplayCont" v-if="visible"> -->
      <div class="fullplayCont" v-show="visible">
        <div class="fillPlayHeader">
          <div class="headerLeft">
            <el-icon
              ><ArrowDownBold
                color="#565565"
                :size="20"
                style="cursor: pointer"
                @click="playerStore.setExpandSong(false)"
            /></el-icon>
          </div>
        </div>
        <div class="playWrapper">
          <div class="effAndLyric">
            <div class="gramophone">
              <AudioVisible :display="visible">
                <div class="blackAlbumBase">
                  <img
                    :src="playerStore?.song?.al?.picUrl"
                    alt=""
                    :class="{ playing: playerStore.isPlaying === true }"
                  />
                </div>
              </AudioVisible>
            </div>
            <div class="lyricCont">
              <div class="songTitle">
                {{ playerStore?.song?.name }}
              </div>
              <div class="songInfos">
                <div>
                  歌手：<span class="expandPageLink">{{
                    formatCreators(playerStore?.song?.ar)
                  }}</span>
                </div>
                <div>
                  专辑：<span class="expandPageLink">{{
                    playerStore?.song?.al?.name
                  }}</span>
                </div>
              </div>
              <ScrollLyric
                :lyric="parsedLrc?.lyrics"
                :song-id="songId"
                :cur-index="lyricCurIndex"
              />
            </div>
          </div>

          <div class="playComment">
            <div class="left">
              <CommentSection
                v-if="visible"
                :comment-source="CommentType['歌曲']"
                :source-id="playerStore.song?.id"
                :expandmode="true"
                :on-to-hot-comments="
                  () => {
                    playerStore.setExpandSong(false);
                  }
                "
              ></CommentSection>
            </div>
            <div class="right"></div>
          </div>
        </div>
      </div>
    </transition>
  </Teleport>
</template>

<script lang="ts">
import { defineComponent, ref, watch, onMounted } from "vue";
import { Lrc, Runner } from "lrc-kit";

import AudioVisible from "@/components/AudioVisible/AudioVisible.vue";
import CommentSectionVue from "@/components/CommentSection/CommentSection.vue";
import { CommentType } from "@/types/comment";
import { usePlayerStores } from "@/stores/player";
import { formatCreators } from "@/utils/utils";
import { useSongLyric } from "@/hooks/commonQueryHook";
import ScrollLyric from "./scrollLyric.vue";

// todo add评论模块 歌词模块
export default defineComponent({
  name: "ExpandPlayPage",
  props: {
    songId: {
      type: [String, Number],
      default: -1,
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    ScrollLyric: ScrollLyric,
    CommentSection: CommentSectionVue,
    AudioVisible: AudioVisible,
  },
  setup(props) {
    const visible = ref(props.visible);
    const playerStore = usePlayerStores();
    const { LyricData, queryLyricData } = useSongLyric();
    const parsedLrc = ref<Lrc>();
    const runner = ref(new Runner());
    const songId = ref<string | number>("");
    const lyricCurIndex = ref(0);

    watch(
      () => props.visible,
      (n) => {
        visible.value = n;
        if (n === true) {
          if (playerStore.audio) {
            playerStore.audio.addEventListener("timeupdate", () => {
              if (playerStore.audio?.currentTime) {
                runner.value.timeUpdate(playerStore.audio?.currentTime);
                lyricCurIndex.value = runner.value.curIndex();
              }
            });
          }
        }
      }
    );

    watch(
      () => playerStore.song,
      (n) => {
        console.log(n);
      }
    );

    watch(
      () => playerStore.song?.id,
      (n) => {
        if (n) {
          queryLyricData(n);
        }
      }
    );

    watch(
      () => LyricData.value,
      (n) => {
        if (n && n.lrc) {
          parsedLrc.value = Lrc.parse(n.lrc?.lyric);
          console.log(parsedLrc.value.lyrics);

          runner.value.setLrc(parsedLrc.value);
        }
      }
    );

    // onMounted(() => {
    //   if (playerStore.audio) {
    //     playerStore.audio.addEventListener("timeupdate", () => {
    //       if (playerStore.audio?.currentTime) {
    //         runner.value.timeUpdate(playerStore.audio?.currentTime);
    //         lyricCurIndex.value = runner.value.curIndex();
    //       }
    //     });
    //   }
    // });

    return {
      visible,
      playerStore,
      formatCreators,
      parsedLrc,
      lyricCurIndex,
      songId,
      CommentType,
    };
  },
});
</script>

<style lang="less">
@keyframes albumRotate {
  0% {
    transform: rotate(0deg);
  }
  25% {
    transform: rotate(90deg);
  }
  50% {
    transform: rotate(180deg);
  }
  75% {
    transform: rotate(270deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
.fullplayCont {
  position: fixed;
  bottom: 76px;
  left: 0;
  right: 0;
  height: calc(100vh - 76px);
  z-index: 10;
  width: 100%;
  overflow-y: auto;
  background: linear-gradient(to bottom, #dbd2d5, #faf9f9, #ffffff);
  &::-webkit-scrollbar {
    width: 5px;
    height: 5px;
    background-color: rgba(255, 255, 255, 0.2);
  }

  &::-webkit-scrollbar-track {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0);
  }

  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0);
    background-color: #cbdfdd;
  }
  .fillPlayHeader {
    height: 46px;
    line-height: 46px;
    .headerLeft {
      padding-left: 28px;
      height: 46px;
      line-height: 46px;
    }
  }
  .fullplayItem {
    height: 200px;
    width: 100%;
    background-color: #e3e3e3;
    border-radius: 8px;
    margin-bottom: 20px;
  }
  .playWrapper {
    width: 920px;
    margin: 0 auto;
    .effAndLyric {
      height: 456px;
      display: flex;
      .gramophone {
        width: 50%;
        height: 100%;
        display: flex;
        align-items: center;
        .blackAlbumBase {
          background-image: url("@/assets/blackalbumbg.png");
          background-repeat: no-repeat;
          width: 300px;
          height: 300px;
          border-radius: 50%;
          // border: 12px solid rgba(255, 255, 255, 0.5);
          background-size: 100% 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          > img {
            width: 204px;
            border-radius: 50%;
            height: 204px;

            animation: albumRotate 40s linear infinite;
            animation-play-state: paused;
            &.playing {
              animation-play-state: running;
            }
          }
        }
      }
      .lyricCont {
        width: 50%;
        height: 100%;
        .songTitle {
          font-size: 26px;
          font-weight: bold;
          color: #333;
          margin-bottom: 8px;
        }
        .songInfos {
          display: flex;
          > div {
            flex: 1;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            color: #999;
            .expandPageLink {
              cursor: pointer;
              &:hover {
                color: blue;
              }
            }
          }
        }
      }
    }
    .playComment {
      margin-top: 16px;
      display: flex;
      .left {
        width: 600px;
      }
      .right {
        flex: 1;
        margin-left: 16px;
      }
    }
  }
}

.fullplay-enter-active,
.fullplay-leave-active {
  transition: height 0.5s ease-out;
}
.fullplay-enter-from,
.fullplay-leave-to {
  height: 0px;
}
</style>
